import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { ChatRoomService } from './chatroom.service';
import { CreateChatRoomDto } from './dto/create-chatroom.dto';
import { JoinChatRoomDto } from './dto/join-chatroom.dto';
import { SearchChatRoomDto } from './dto/search-chatroom.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('chatrooms')
@UseGuards(JwtAuthGuard)
export class ChatRoomController {
  constructor(private chatRoomService: ChatRoomService) {}

  @Post()
  async createChatRoom(
    @Request() req,
    @Body() createChatRoomDto: CreateChatRoomDto,
  ) {
    return this.chatRoomService.createChatRoom(req.user.id, createChatRoomDto);
  }

  @Get('search')
  async searchChatRooms(@Query() searchDto: SearchChatRoomDto) {
    return this.chatRoomService.searchChatRooms(searchDto);
  }

  @Get('my')
  async getMyChatRooms(@Request() req) {
    return this.chatRoomService.getUserChatRooms(req.user.id);
  }

  @Get(':id')
  async getChatRoom(@Param('id') id: string, @Request() req) {
    return this.chatRoomService.getChatRoomById(parseInt(id), req.user.id);
  }

  @Post(':id/join')
  async joinChatRoom(
    @Param('id') id: string,
    @Request() req,
    @Body() joinDto: JoinChatRoomDto,
  ) {
    return this.chatRoomService.joinChatRoom(
      parseInt(id),
      req.user.id,
      joinDto,
    );
  }

  @Delete(':id/leave')
  async leaveChatRoom(@Param('id') id: string, @Request() req) {
    return this.chatRoomService.leaveChatRoom(parseInt(id), req.user.id);
  }
}
